<template>
	<view class="content">
		<view class="header">
			<!-- 搜索 -->
			<view class="search">
				<input class="uni-input" focus placeholder="请输入店铺名称" @input='shopValue' />
				<view class="screen">
					<view class="icon">
						<image src="https://wx.caobenjiankang.com/image/finance/screen.png" mode="heightFix"></image>
					</view>
					<view class="font24" @click="getData">
						筛选
					</view>
				</view>
			</view>
			<!-- 商铺信息 -->
			<view class="shop">
				<view class="image">
					<image :src="shopData.business_license" mode="aspectFit"></image>
				</view>
				<view class="shopInfo">
					<view class="title font30 fontw">
						{{shopData.title}}
					</view>
					<view class="phone font24">
						简介:{{shopData.attestation}}
					</view>
				</view>
			</view>
		</view>
		<!-- 订单 -->
		<view class="box">
			<view class="purchase">
				<scroll-view :scroll-top="scrollTop" scroll-y="true" class="scroll-Y" @scrolltolower="scrolltolower">
					<view class="item" v-for="(item,index) in shopData.storeMate" :key="item.mate_id">
						<view class="left">
							<view class="image">
								<image :src="item.mate_info.img" mode="aspectFit"></image>
							</view>
							<view class="info">
								<view class="title font30 font500">
									{{item.mate_info.name}}
								</view>
								<view class="stock font18">
									库存{{item.num}}
								</view>
							</view>
						</view>
						<view class="purchaseB">
							<view class="purchaseT font26" @click="buy(item)">
								采购
							</view>
							<view class="tips font18" v-if="item.num < 10">
								库存不足<br>请补货
							</view>
						</view>
					</view>
				</scroll-view>
			</view>
		</view>
		<u-popup :show="show" mode="center" border-radius="14" :mask-close-able="false" :closeable="true" @close="close">
			<view class="p_box">
				<view class="title">
					{{p_data.name}}
				</view>
				<view class="des">
					补充物料：
				</view>
				<view class="numBox">
					<view class="" @click="jian">
						<u-icon name="minus-circle" color="#2979ff" size="20"></u-icon>
					</view>
					<u--input placeholder="请输入内容" border="surround" v-model="value" @change="change"></u--input>
					<view class="" @click="jia">
						<u-icon name="plus-circle" color="#2979ff" size="20"></u-icon>
					</view>
				</view>
				<view class="btn" @click="submit(shopData.id,p_data.id)">
					提交
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				show: false,
				scrollTop: 0, //初始化滚动高度
				value: 0,
				shopData: {

				}, //商铺信息
				keyword: '',
				s_data: {},
				p_data: {},
			}
		},
		onLoad(op) {
			this.id = op.id
			this.getData(op.id)
		},
		methods: {
			async getData(id) {
				const data = await this.$api.finance.storeMate({
					storeid: 1,
					keyword: this.keyword
				})
				data.storeMate.map(item => {
					item = JSON.parse(JSON.stringify(item))
					return item
				})
				this.shopData = data
			},
			shopValue(e) {
				this.keyword = e.detail.value
			},
			// 滚动到底部
			scrolltolower() {},
			buy(item) {
				this.show = true
				this.s_data = item
				this.p_data = item.mate_info
			},
			close() {
				this.show = false
				this.value = 0
				this.getData(this.id)
			},
			change(e) {
				let that = this
				if (e > that.p_data.num) {
					that.value = that.s_data.num
					uni.showToast({
						icon: 'error',
						title: "超出数量",
						duration: 1000
					})
					return
				}
				if (e < that.p_data.num) {
					that.value = e
				}
			},
			jian() {
				let that = this
				if (that.value == 0) {
					return
				}
				that.value = Number(that.value) - 1
			},
			jia() {
				let that = this
				if (that.value == that.p_data.num) {
					that.value = Number(that.value) - 1
					return
				}
				that.value = Number(that.value) + 1
			},
			async submit(sid, mid) {
				const data = await this.$api.finance.mateSave({
					storeid: sid,
					mateid: mid,
					num: this.value
				})
				if (data) {
					uni.showToast({
						icon: 'error',
						title: "补充成功",
						duration: 1000
					})
					this.show = false
					this.value = 0
					this.getData(this.id)
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		.header {
			background-color: #fff;
			width: 100%;

			.search {
				padding: 16rpx 43rpx;
				display: flex;
				justify-content: space-between;
				align-items: center;

				.uni-input {
					flex: 1;
					height: 66rpx;
					padding-left: 36rpx;

					/deep/.input-placeholder {
						font-size: 24rpx
					}
				}

				.screen {
					display: flex;
					align-items: center;

					.icon {
						margin-right: 8rpx;

						image {
							height: 21rpx;
						}
					}
				}
			}

			.shop {
				padding: 12rpx 52rpx;

				.image {
					border-radius: 24rpx;
					overflow: hidden;
					margin-bottom: 38rpx;

					image {
						width: 100%;
						height: 240rpx;
					}
				}

				.shopInfo {
					.title {
						display: -webkit-box;
						-webkit-line-clamp: 1;
						/* 显示的行数 */
						-webkit-box-orient: vertical;
						overflow: hidden;
					}

					.phone {
						color: #666666;
						display: -webkit-box;
						-webkit-line-clamp: 3;
						/* 显示的行数 */
						-webkit-box-orient: vertical;
						overflow: hidden;
					}
				}
			}
		}

		.box {
			padding: 0 52rpx;
			margin-top: 16rpx;

			// 订单
			.purchase {
				.scroll-Y {
					height: calc(100vh - var(--status-bar-height) - 98rpx - 189rpx);

					.item {
						display: flex;
						justify-content: space-between;
						align-items: center;
						padding: 21rpx 33rpx 33rpx 33rpx;
						margin-bottom: 16rpx;
						background-color: #ffffff;
						border-radius: 24rpx;

						.left {
							.image {
								width: 121rpx;
								height: 121rpx;
								border-radius: 24rpx;
								overflow: hidden;

								image {
									width: 121rpx;
									height: 121rpx;
								}
							}

							.info {
								margin-left: 20rpx;

								.title {
									color: #333333;
									margin-bottom: 19rpx;
								}

								.stock {
									color: #666666;
								}
							}
						}

						.purchaseB {
							display: flex;
							flex-direction: column;
							justify-content: center;
							align-items: center;

							.purchaseT {
								width: 133rpx;
								height: 52rpx;
								color: #FFFFFF;
								background: linear-gradient(0deg, #2AB883 0%, #89D894 100%);
								border-radius: 20rpx;
								text-align: center;
								line-height: 52rpx;
							}

							.tips {
								text-align: center;
								color: #FF6000;
							}
						}
					}
				}
			}
		}

		.p_box {
			padding: 24rpx;

			.title {
				text-align: center;
				margin-bottom: 32rpx;
			}

			.des {
				margin-bottom: 16rpx;
			}

			.numBox {
				display: flex;
				align-items: center;
				margin-bottom: 32rpx;

				/deep/.u-input {
					margin: 0 8rpx;
				}
			}

			.btn {
				height: 52rpx;
				color: #FFFFFF;
				background: linear-gradient(0deg, #2AB883 0%, #89D894 100%);
				border-radius: 20rpx;
				text-align: center;
				line-height: 52rpx;
			}
		}

		/deep/.u-safe-bottom {
			display: none !important;
		}
	}
</style>